package org.blog.api;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.blog.common.ResultVO;
import org.blog.dto.BlogEssayDTO;
import org.blog.entity.BlogEssay;
import org.blog.service.BlogEssayService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@Api("博客文章模块")
@RequestMapping("/api/essay")
public class BlogEssayController {
    @Resource
    private BlogEssayService blogEssayService;

    @ApiOperation("获取所有文章")
    @GetMapping("/getAll")
    public ResultVO getAll(String blogId) {
        log.info("获取所有文章接口入参：{}", blogId);
        List<BlogEssay> blogEssayList = blogEssayService.getBlogEssays();
        log.debug("获取所有文章接口出参：{}", JSON.toJSONString(blogEssayList));
        return ResultVO.success(blogEssayList);
    }

    @ApiOperation("保存文章")
    @PostMapping("/saveEssay")
    public ResultVO saveBlogEssay(BlogEssayDTO blogEssayDTO) {
        log.info("保存文章接口入参：{}", blogEssayDTO);
        String essayId = blogEssayService.saveBlogEssay(blogEssayDTO);
        log.debug("保存文章接口出参：{}", JSON.toJSONString(essayId));
        return ResultVO.success(essayId);
    }
}
